<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->database();
	}

	public function index()
	{
		$header_data = array();
		$footer_data = array();
		$detail_data = array();

		//title
		$header_data['title'] = 'Admin--category mgt--';

		//add css
		$header_data['add_css'][] = '/zswebshop/include/css/bootstrap-extends.css';
		$header_data['add_css'][] = '/zswebshop/include/css/jquery.treegrid.css';
		$header_data['add_css'][] = '/zswebshop/include/admin/css/category.css';

		//add script
		$footer_data['add_script'][] = '/zswebshop/include/js/jquery.treegrid.js';
		$footer_data['add_script'][] = '/zswebshop/include/js/jquery.treegrid.bootstrap3.js';
		$footer_data['add_script'][] = '/zswebshop/include/admin/js/category.js';

		// tree data
		$detail_data['category_depth_limit'] = $this->config->item('category_depth_limit');

		$this->load->model('/admin/category_mo', 'category_mo');
		$detail_data['category_tree'] = $this->category_mo->get_all_category('0', 1);

		$this->load->view('admin/common/header', $header_data);
		$this->load->view('/admin/category/category_vi', $detail_data);
		$this->load->view('admin/common/footer', $footer_data);
	}


	public function get_sub_category($category_code)
	{
		$result = array();

		$error_message = '';

		$this->load->model('/admin/category_mo', 'category_mo');
		$result['data'] = $this->category_mo->get_sub_category_list($category_code);

		if (!isset($result['data'])) {
			$error_message = 'Query result is empty.';
		}

		$result['error'] = $error_message;

		echo json_encode($result);
	}


	public function add_category()
	{
		$result = array();

		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');

		// load form_validation
		$this->lang->load('form_validation', 'korean');
		$this->lang->load('form_validation_label', 'korean');

		$this->form_validation->set_error_delimiters('', '');

		if ($this->form_validation->run() == FALSE)
		{
			$validation_error['before_parent_category_code'] = form_error('before_parent_category_code');
			$validation_error['category_code'] = form_error('category_code');
			$validation_error['category_name_pc'] = form_error('category_name_pc');

			$result['error'] = $validation_error;
		}
		else
		{
			$db_param = array();

			$db_param['parent_category_code'] = $this->input->post('before_parent_category_code');
			$db_param['category_code'] = $this->input->post('category_code');
			$db_param['category_name_pc'] = $this->input->post('category_name_pc');
			$db_param['category_depth_limit'] = $this->config->item('category_depth_limit');

			$this->load->model('/admin/category_mo', 'category_mo');
			$query = $this->category_mo->insert_category($db_param);

			if ($query != 0)
			{
				$result['error'] = array('db_error'=>'카테고리 등록중 이상이 발생하였습니다. [level: ' . $query . ']');
			}
			else
			{
				$result['data'] = array('url'=>'./category');
			}
		}


		echo json_encode($result);
	}



	public function del_category()
	{
		$result = array();

		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');

		// load form_validation
		$this->lang->load('form_validation', 'korean');
		$this->lang->load('form_validation_label', 'korean');

		$this->form_validation->set_error_delimiters('', '');

		if ($this->form_validation->run() == FALSE)
		{
			$validation_error['category_code'] = form_error('category_code');

			$result['error'] = $validation_error;
		}
		else
		{
			$db_param = array();

			$db_param['category_code'] = $this->input->post('category_code');

			$this->load->model('/admin/category_mo', 'category_mo');
			$query = $this->category_mo->delete_category($db_param);

			if ($query != 0)
			{
				$result['error'] = array('db_error'=>'카테고리 삭제중 이상이 발생하였습니다. [level: ' . $query . ']');
			}
			else
			{
				$result['data'] = array('url'=>'./category');
			}
		}


		echo json_encode($result);
	}


	public function change_priority()
	{
		$result = array();

		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');

		$this->lang->load('form_validation', 'korean');
		$this->lang->load('form_validation_label', 'korean');

		//$this->form_validation->set_error_delimiters('<div class="form-group has-error"><label class="control-label" for="inputError">', '</label></div>');
		$this->form_validation->set_error_delimiters('', '');

		if ($this->form_validation->run() == FALSE)
		{
			$validation_error['category_code[]'] = form_error('category_code[]');
			$validation_error['display_order[]'] = form_error('display_order[]');
			$validation_error['display_order'] = form_error('display_order');

			$result['error'] = $validation_error;
		}
		else
		{
			$db_param = '';

			$p_category_code = $this->input->post('category_code');
			$p_display_order = $this->input->post('display_order');

			$cnt = 0;
			foreach ($p_category_code as $val)
			{
				$db_param .= ($p_display_order[$cnt] . ':' . $val);

				if (isset($p_display_order[$cnt + 1]))
				{
					$db_param .= '__';
				}

				$cnt++;
			}

			$this->load->model('/admin/category_mo', 'category_mo');
			$query = $this->category_mo->update_display_order($db_param);

			if ($query == NULL or $query != 0)
			{
				$result['error'] = array('db_error'=>'순위변경중 이상이 발생하였습니다. [level: ' . $query . ']');
			}
			else
			{
				$result['data'] = array('url'=>'./category');
			}
		}

		echo json_encode($result);
	}


	function _is_priority($display_order)
	{
		if (count($display_order) <= 0)
		{
			$this->form_validation->set_message('_is_priority', '"%s" 항목에 값이 없습니다.');
			return FALSE;
		}
		else if (count($display_order) <= 1)
		{
			$this->form_validation->set_message('_is_priority', '"%s" 항목이 2개이상 필요합니다.');
			return FALSE;
		}
		else if (count($display_order) !== count(array_unique($display_order)))
		{
			$this->form_validation->set_message('_is_priority', '"%s" 항목의 값이 중복되었습니다.');
			return FALSE;
		}

		return TRUE;
	}




	function test_proc()
	{
		//$db_param = '2:E101749__1:E101747';
		$db_param['parent_category_code'] = 'E101745';
		$db_param['category_code'] = 'E101753';
		$db_param['category_name_pc'] = 'TEST111111';
		$db_param['category_depth_limit'] = $this->config->item('category_depth_limit');
		$this->load->model('/admin/category_mo', 'category_mo');
		$query = $this->category_mo->delete_category($db_param);
		echo $query;
	}
}

?>